package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
/**
 * ListView添加头添加尾
 * addHeader:添加头
 * addFooter:添加尾
 *
 *
 * xml----View.layoutInflater
 * */
public class ListView_Activity extends AppCompatActivity {
    private ListView listView1;
    private ArrayAdapter<String> arrayAdapter;
    private List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_);
        listView1=findViewById(R.id.list1);
        list=new ArrayList<String>();
        for(int i=0;i<20;i++){
            list.add("数据元素"+i);
        }
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
       View header=LayoutInflater.from(this).inflate(R.layout.header,null);

       listView1.addHeaderView(header);
       //动态给定控件,第二种方法
       // Button footer=new Button(this);
      //  footer.setText("加载更多");
      //  listView1.addFooterView(footer);
        View footer=LayoutInflater.from(this).inflate(R.layout.footer,null);
        listView1.addFooterView(footer,null,true);
        //显示适配器
       listView1.setAdapter(arrayAdapter);
    }
}
